<template>
  <div class="list">
    <div class="item">
      <el-form-item label="输入问题名称：" required>
        <el-input v-model="nowData.name" placeholder="请输入问题名称"></el-input>
        <el-button type="primary" plain size="mini" style="margin-left: 10px" @click="addOption(index)">
          +添加选项
        </el-button>
      </el-form-item>
      <div v-for="(x, xIndex) in nowData.options" :key="xIndex">
        <el-form-item label="输入选项：" required>
          <div style="display: flex">
            <el-input v-model="x.name" placeholder="请输入选项" style="width: 200px; margin-right: 10px"></el-input>
            <el-button type="danger" plain size="mini" @click="deleteItem(xIndex)">删除</el-button>
            <el-button type="primary" plain size="mini" @click="addSonItem(xIndex)">+添加子选项</el-button>
            <el-button type="primary" plain size="mini">+添加材料</el-button>
            <div class="dataBox">
              <span>关联材料：</span>
              <a href="javascript:;" v-for="(y, yIndex) in x.material" :key="yIndex">{{ y.name }}</a>
            </div>
          </div>
        </el-form-item>
        <div v-if="x.children && x.children.length != 0">
          <div v-for="(z, zIndex) in x.children" :key="zIndex" class="sonBox">
            <i class="el-icon-circle-close deleteSon" @click="deleteSon(xIndex, zIndex)"></i>
            <List :nowData="z"></List>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'List',
    props: {
      nowData: {
        type: Object,
        default: () => {}
      },
      index: {
        type: Number,
        default: 0
      }
    },
    data() {
      return {};
    },

    methods: {
      addOption() {
        //添加新选项
        this.nowData.options.push({
          name: '',
          material: [],
          children: []
        });
      },
      deleteItem(xIndex) {
        //删除当前选项
        this.nowData.options.splice(xIndex, 1);
      },
      addSonItem(xIndex) {
        //添加子选项
        this.nowData.options[xIndex].children.push({
          name: '',
          options: [
            {
              name: '',
              material: [],
              children: []
            }
          ]
        });
      },
      deleteSon(xIndex, zIndex) {
        //删除当前子选项组
        this.nowData.options[xIndex].children.splice(zIndex, 1);
      }
    },
    mounted() {}
  };
</script>

<style lang="scss" scoped>
  .list {
    padding: 20px;
    background: rgba(90, 145, 184, 0.1);
    .item {
      padding: 10px 0;
    }
    .sonBox {
      margin-bottom: 5px;
      position: relative;
      .deleteSon {
        position: absolute;
        right: 5px;
        top: 5px;
        font-size: 20px;
        color: #d11f1f;
        cursor: pointer;
      }
    }
  }
  .dataBox {
    margin-left: 10px;
    a {
      color: #409eff;
      margin-right: 5px;
    }
  }
</style>
